package com.woniu.adapter;

import com.woniu.client.WorkClient;
import com.woniu.inlet.vo.WorkingProcedureVo;
import com.woniu.outlet.dao.ProSchedulingPoMapper;
import com.woniu.outlet.dao.ScheWorkMapper;
import com.woniu.outlet.po.*;
import com.woniu.uitl.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Component
@Transactional
public class ScheWorkAdapter {

    @Autowired
    private ScheWorkMapper scheWorkMapper;
    @Autowired
    private ProSchedulingPoMapper proSchedulingPoMapper;
    @Autowired
    private WorkClient systemClient;

    //为生产排程添加工序
    public void addWorking(Long proSche,String workProCode) throws Exception{
        //判断生产排程存不存在
        ProSchedulingPo pro = proSchedulingPoMapper.selectByPrimaryKey(proSche);
        if (StringUtils.isEmpty(pro)){
            throw new Exception("无效的生产排程id");
        }
        //判断工艺工序存在
        ResponseResult<WorkingProcedureVo> byCode = systemClient.getByCode(workProCode);
        WorkingProcedureVo workPro = byCode.getData();

        if (StringUtils.isEmpty(workPro)){
            throw new Exception("无效的工序编码");
        }
        //判断该生产排程是否有此次工艺安排
        ScheWorkExample scheWorkExample = new ScheWorkExample();
        ScheWorkExample.Criteria criteria = scheWorkExample.createCriteria();
        criteria.andWorkProIdEqualTo(workPro.getId());
        criteria.andProScheIdEqualTo(proSche);
        List<ScheWork> scheWorks = scheWorkMapper.selectByExample(scheWorkExample);
        if (scheWorks.size() != 0){
            throw new Exception("重复的工序添加");
        }else {
            ScheWork scheWork = new ScheWork();
            scheWork.setProScheId(proSche);
            scheWork.setWorkProId(workPro.getId());
            scheWork.setProNum(pro.getOrderNum() * workPro.getProSpecifications());
            scheWorkMapper.insertSelective(scheWork);
        }
    }

    //为生产排程删除工序
    public void deleteWorking(Long proSche,String workProCode) throws Exception{
        //判断生产排程存不存在
        ProSchedulingPo pro = proSchedulingPoMapper.selectByPrimaryKey(proSche);
        if (StringUtils.isEmpty(pro)){
            throw new Exception("无效的生产排程id");
        }
        //判断工艺工序存在
        ResponseResult<WorkingProcedureVo> byCode = systemClient.getByCode(workProCode);
        WorkingProcedureVo workPro = byCode.getData();
        if (StringUtils.isEmpty(workPro)){
            throw new Exception("无效的工序id");
        }
        //判断该排程是否有这到工序
        ScheWorkExample scheWorkExample = new ScheWorkExample();
        ScheWorkExample.Criteria criteria = scheWorkExample.createCriteria();
        criteria.andWorkProIdEqualTo(workPro.getId());
        criteria.andProScheIdEqualTo(proSche);
        List<ScheWork> scheWorks = scheWorkMapper.selectByExample(scheWorkExample);
        if (scheWorks.size() == 0){
            throw new Exception("无效的删除");
        }else {
            scheWorkMapper.deleteByExample(scheWorkExample);
        }
    }

}
